package com.fulihui.weixinsdk.request.material;

import com.alibaba.fastjson.JSON;
import com.fulihui.weixinsdk.http.HttpMethodEnum;
import com.fulihui.weixinsdk.request.WeixinRequest;
import com.fulihui.weixinsdk.result.material.MaterialAddResult;
import com.google.gson.Gson;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.File;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;


/**
 * 新增其他类型永久素材-请求对象
 *
 * @author wang_wx
 * @date 2018-07-11
 */
@Getter
@Setter
@ToString
public class MaterialAddRequest extends WeixinRequest<MaterialAddResult> {

    private transient String access_token;

    /**
     * 媒体文件类型，分别有图片（image）、语音（voice）、视频（video）和缩略图（thumb）
     */
    private String type;

    /**
     * form-data中媒体文件
     */
    private File media;


    @Override
    public File file() {
        return getMedia();
    }

    @Override
    public Map<String, String> urlParam() {
        Map<String, String> param = new TreeMap<>();
        if (!Objects.isNull(access_token)) {
            param.put("access_token", access_token);
            param.put("type", type);
        }
        return param;
    }

    @Override
    protected void childParam() {

    }

    @Override
    public String service() {
        return "https://api.weixin.qq.com/cgi-bin/material/add_material";
    }

    @Override
    public String urlEndStr() {
        return null;
    }

    @Override
    public MaterialAddResult parseResult(String respStr) {
        return new Gson().fromJson(respStr, MaterialAddResult.class);
    }

    @Override
    public HttpMethodEnum httpMethod() {
        return HttpMethodEnum.MULTIPART_POST;
    }

    @Override
    public String requestData() {
        return JSON.toJSONString(this);
    }
}
